package main

import (
	"fmt"

	"github.com/zeromicro/go-zero/core/stores/redis"
)

func main() {
	conf := redis.RedisConf{
		Host: "10.0.0.161:6379",
		Type: "node",
		Pass: "",
		Tls:  false,
	}

	rds := redis.MustNewRedis(conf)

	lock := redis.NewRedisLock(rds, "test")

	// 设置过期时间
	lock.SetExpire(10)

	// 尝试获取锁
	acquire, err := lock.Acquire()

	switch {
	case err != nil:
		// deal err
	case acquire:
		// 获取到锁
		fmt.Println("get lock")

		// 业务逻辑

		lock.Release() // 释放锁
		fmt.Println("unlock")
	case !acquire:
		// 没有拿到锁
		fmt.Println("did not get the lock")
	}

}
